package com.nd.te;

import java.io.*;

public class ReadCSVAndWriteTxt {
    public void test(int col,String fileinpath,String fileoutpath){
        try {
            BufferedReader reader = new BufferedReader(new FileReader(fileinpath));
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileoutpath,true)));
            String line = null;
//            int index = 0;
            while((line=reader.readLine())!=null){
                String item[] = line.split(",");
//                if(index < 200){
//                    if(item.length>=col-1){
//                        String last = item[col-1];
//                        System.out.print(index);
//                        System.out.print(",");
//                        System.out.println(last);
//                    }
//                }
//                index++;
                if(item.length>=col-1){
                    String last = item[col-1];
                    out.write(last+"\r\n");
                }
            }
            out.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void test1(String fileinpath, String fileoutpath){
        try {
            BufferedReader reader = new BufferedReader(new FileReader(fileinpath));
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileoutpath,true)));
            String line = null;
            int index = 0;
            while((line=reader.readLine())!=null){
                String item[] = line.split(",");
                if(index==0){
                    index++;
                    continue;
                }
                String last = item[0];
                int temp = Integer.parseInt(last);
                if(temp!=2020){
                    temp = temp - temp % 10 + 10;
                }
                out.write(String.valueOf(temp));
                out.write(",");


                last = item[1];
                out.write(last);
                out.write(",");

                last = item[5];
                out.write(last);
                out.write(",");

                last = item[7];
                out.write(last);
                out.write(",");

                last = item[8];
                out.write(last);
                out.write(",");

                last = item[9];
                out.write(last);
                out.write(",");

                last = item[10];
                out.write(last);
                out.write(",");

                last = item[11];
                out.write(last);
                out.write(",");

                last = item[25];
                out.write(last);
                out.write("\n");
            }
            out.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void test2(String fileinpath,String fileoutpath){
        try {
            BufferedReader reader = new BufferedReader(new FileReader(fileinpath));
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileoutpath,true)));
            String line = null;
            String last = null;
            out.write("[");
            while((line=reader.readLine())!=null){
                String item[] = line.split(",");
                if(item[10]!=null && item[11]!=null){
                    out.write("{\"latitude\": "+item[10]+","+"\"longitude\": "+item[11]+"},");
                }
            }
            out.write("]");
            out.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        String fileinpath="C:\\Users\\张传畅\\Desktop\\bigwork\\1.csv";
        String fileoutpath="C:\\Users\\张传畅\\Desktop\\bigwork\\weapon.txt";
        ReadCSVAndWriteTxt test = new ReadCSVAndWriteTxt();
        test.test1(fileinpath,fileoutpath);
    }
}
